package com.example.toutiao.ui.main.adpater

import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.TextView
import androidx.recyclerview.widget.GridLayoutManager
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.example.toutiao.R
import com.example.toutiao.ui.main.model.AddressModel
import com.example.toutiao.ui.main.model.CommonModel

class AddressAdapter(mAddress:MutableList<AddressModel>):RecyclerView.Adapter<AddressAdapter.AddressHolder>() {
    interface OnClickListener{
        fun OnClick(title:String)
    }
    private var mOnClickListener:OnClickListener?=null
    fun setOnClickListener(mOnClickListener:OnClickListener){
        this.mOnClickListener=mOnClickListener
    }
    private val mCommonList:MutableList<AddressModel>?=mAddress

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): AddressHolder=
        /**
         * 加载需要的布局
         */
        AddressHolder(LayoutInflater.from(parent.context).inflate(R.layout.item_address,parent,false))

    override fun onBindViewHolder(holder: AddressHolder, position: Int) {
        if(mCommonList!=null){
            val mCommonModel=mCommonList[position]
            holder.mTitle.text=mCommonModel.name
            holder.itemView.setOnClickListener {
                mOnClickListener!!.OnClick(mCommonModel.name)
            }
        }
    }

    override fun getItemCount(): Int =mCommonList!!.size

    /**
     * 作用是拿到view
     */
    class AddressHolder(it: View):RecyclerView.ViewHolder(it){
        /*通过适配器 在通过id 拿到对应的视图组件*/
        val mTitle:TextView=it.findViewById(R.id.tv_title)
    }
}